let actions = {
  'message': package => {
    app.messageList.push(package);
  },

  'player.add.max': () => {
    app.linkStatus = false;
    app.msgStatus = true;
    view.play('home');
  },
  'player.add.error': () => {
    setTimeout(() => {
      app.sendUsername();
    }, 0);
  },

  'room.add': package => {
    app.currentRoomInfo = package.data.roomInfo;
    app.currentRoomInfo.masterName = app.currentRoomInfo.playerList.filter(i => i.id == app.currentRoomInfo.master)[0].username;
  },
  'room.leave': () => {
    view.play('home');
  },
  'room.add.error': package => {
    setTimeout(() => {
      view.play('inputRoomKey');
    }, 0);
  },
  'room.list': package => {
    let result = [];
    let data = {};
    package.data.forEach(item => {
      let master = item.playerList.filter(i => i.id == item.master)[0];
      if(master) data.username = master.username;
      data.maxPlayerNum = item.maxPlayerNum;
      data.playerNum = item.playerList.length;
      data.key = item.key;
      data.isGame = item.isGame;
      result.push(data);
    })
    app.roomList = result;
  },

  'game.create': package => {
    view.loading('创建游戏中...', success => {
      actionCallFn['game.create.end'] = success;
    }, 'game');
  },
  'game.start': package => {
    game.set(package.data.game);
    package.data.room.playerList.forEach(item => {
      // 将其置入游戏中
      game.add(item, package.data.game);
    })
  },
  'game.action': package => {
    game.complite(package.data);
  },

  'jump': package => {
    setTimeout(() => {
      view.play(package.data.view);
    }, 0);
  }
}

let actionCallFn = {};

function parseAction(data) {
  let flag = false;
  if (actionCallFn[data.action]) {
    flag = true;
    actionCallFn[data.action]();
    delete actionCallFn[data.action];
  }

  if (!data.action) {
    console.warn('无法处理的数据包', data);
    return;
  }
  let action = actions[data.action];
  if (!action) {
    !flag && console.warn('无法处理的动作: ' + data.action);
    return;
  }
  
  actions[data.action](data);
}